<?php
/**
 * 购物车管理
 * author:ty
 * date:2017/08/29
 */

namespace App\Http\Controllers;

use App\Http\Models\Address;
use App\Http\Models\Coupon;
use App\Http\Models\Fitment;
use App\Http\Models\Shopcart;
use App\Http\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class ShopCartController extends Controller
{
    public $nav = null;
    public $side = null;
    public $addr = null;
    public $shopcart = null;
    public $fitment = null;
    public $color = null;//公用颜色配置
    public $coupon = null;//优惠券操作
    public function __construct()
    {
        $this->nav = '家具租赁';
        $this->addr = new Address();
        $this->shopcart = new Shopcart();
        $this->fitment = new Fitment();
        $this->coupon = new Coupon();
        $this->color = CommonController::commmon_color_path();
    }


    /**购物车列表*/
    public function index()
    {
        $this->side = '购物车';
        $this->user =User::find(session('user')->id);
        $param['cart'] = CommonController::shopcart(session('user')->id);
        $param['color'] = $this->color;

        return view('shopcart',['user'=>$this->user,'nav'=>$this->nav,'param'=>$param]);
    }

    /**获取商品关联详情*/
    public function fitment_dts($pid)
    {
        $cart_id = Input::get('cart_id');
        $res = $this->fitment->fitment_dts($pid);
        if(count($res)>0){
            $result = ['res'=>'ok','msg'=>'查询成功!','data'=>$res,'cart_id'=>$cart_id];
        }else{
            $result = ['res'=>'error','msg'=>'没有数据!'];
        }
        echo json_encode($result);
    }

    /**购物车添加*/
    public function add()
    {
        $input = Input::except('_token');
        $param =[
            'cart_fitment_id' => trim($input['details_id']),
            'cart_user_id' => trim(session('user')->id),
            'cart_fitment_num' => trim($input['num']),
            'cart_rent_type' => trim($input['rent_type']),
            'cart_use_time' => trim($input['use_date']),
            'created_at' => date('Y-m-d H:i:s',time()),
            'updated_at' => date('Y-m-d H:i:s',time())
        ];
        /**数据校验*/
        if(!isset($param['cart_fitment_id']) && $param['cart_fitment_id'] == ''){
            $result = ['res'=>'error','msg'=>'商品详情不能为空!'];return json_encode($result);
        }
        if(!isset($param['cart_fitment_num']) && $param['cart_fitment_num'] == ''){
            $result = ['res'=>'error','msg'=>'租赁数量不能为空!'];return json_encode($result);
        }
        if(!isset($param['cart_rent_type']) && $param['cart_rent_type'] == ''){
            $result = ['res'=>'error','msg'=>'请选择租赁方式!'];return json_encode($result);
        }
        if(!isset($input['use_date']) && $input['use_date'] == ''){
            $result = ['res'=>'error','msg'=>'请选择租赁日期!'];return json_encode($result);
        }
        if(date('Y-m-d',strtotime($param['cart_use_time'])) < date('Y-m-d',strtotime('+3 days'))){
            $result = ['res'=>'error','msg'=>'请选择租赁日期不晚于当前3天!'];return json_encode($result);
        }
        $res = $this->shopcart->add($param);
        $url  = url('/immediatorder/'.$res);
        if($res>0){
            $result = ['res'=>'ok','msg'=>'加入购物车成功!','redirect'=>$url];return json_encode($result);
        }else{
            $result = ['res'=>'error','msg'=>'加入购物车失败!'];return json_encode($result);
        }
    }

    /**
     * 组合购物车
     * @dt加入商品详情
     * @固定租，延后3天
     */
    public function add_combine()
    {
        $input = Input::get('jsondata');
        $url  = url('/confirmorder');
        $param = array(
            'cart_user_id' => trim(session('user')->id),
            'cart_fitment_num' => '1',
            'cart_rent_type' => '2',
            'cart_use_time' => date('Y-m-d H:i:s',strtotime('+3 days')),
            'created_at' => date('Y-m-d H:i:s',time()),
            'updated_at' => date('Y-m-d H:i:s',time()));
         DB::beginTransaction();
         try{
             foreach ($input as $key=>$value){
                 $param['cart_fitment_id'] = $value;
                 $res = $this->shopcart->add($param);
                 if($res<1) {
                     DB::rock();
                     $result = ['res'=>'error','msg'=>'组合入库商品失败!','redirect'=>$url];return json_encode($result);
                 }
             }
             DB::commit();
             $result = ['res'=>'ok','msg'=>'加入购物车成功!','redirect'=>$url];return json_encode($result);
         }catch (Exception $e){
             DB::rollback();
             $result = ['res'=>'error','msg'=>'加入购物车失败!'];return json_encode($result);
         }
    }

    /**
     * 立即租赁
     * 组合和单个
     */
    public function immediatorder($res)
    {
        /**单个商品*/
        if(!is_array($res)){
            $this->side = '购物车';
            $this->user =User::find(session('user')->id);
            $param['cart'] = CommonController::shopcart(session('user')->id);
            /**购物车数据*/
            $this->cart = Shopcart::where('cart_user_id',session('user')->id)->get();
            $param['cartlist'] = $this->cart;
            /**收货地址查看*/
            $param['addr'] = $this->addr->lists(session('user')->id);
            /**立即下单数据*/
            $param['immediate'] = $this->shopcart->cartdt($res);
            /**信用额度*/
            $param['user_quota']=DB::table('quota')->where('quota_user_id',session('user')->id)->first();
            /**有效优惠券*/
            $counpon_param['userid']=session('user')->id;
            $param['confirm_coupon'] = $this->coupon->coupon_useable($counpon_param);
            /**颜色*/
            $param['color'] = $this->color;

            return view('confirmorder',['user'=>$this->user,'nav'=>$this->nav,'param'=>$param]);
        }/**组合商品*/
        else{
            return '组合';
        }

    }

   /**
    * 购物车编辑
    * @id 购物车id
    */
   public function edit($id)
   {
       $num = trim(input::get('num'));
       $cart_rent_type = trim(Input::get('cart_rent_type',''));
       $cart_fitment_id = trim(Input::get('cart_fitment_id',''));
       $cart_status=trim(Input::get('cart_status',''));
       $dtid = Input::get('dtid');
       if(isset($num) && $num != ''){
           $data = ['cart_fitment_num'=>$num,'updated_at'=>date('Y-m-d H:i:s',time())];
       }
       if(isset($cart_rent_type) && $cart_rent_type != ''){
           $data = ['cart_rent_type'=>$cart_rent_type,'updated_at'=>date('Y-m-d H:i:s',time())];
       }
       if(isset($cart_fitment_id) && $cart_fitment_id != ''){
           $data = ['cart_fitment_id'=>$cart_fitment_id,'updated_at'=>date('Y-m-d H:i:s',time())];
       }
       if(isset($dtid) && $dtid != ''){
           $data = ['cart_fitment_id'=>$dtid,'updated_at'=>date('Y-m-d H:i:s',time())];
       }
       if(isset($cart_status) && $cart_status != ''){
           $data = ['cart_status'=>$cart_status,'updated_at'=>date('Y-m-d H:i:s',time())];
       }
       $res = $this->shopcart->edit($id,$data);
       if($res){
           $result = ['res'=>'ok','msg'=>'购物车更新成功!'];
           return json_encode($result);
       }else{
           $result = ['res'=>'error','msg'=>'更新失败!'];
           return json_encode($result);
       }
   }

   /**当前用户购物车全选*/
   public function edit_status()
   {
       $status = Input::get('status');
       if($status==1){
           $res = DB::table('cart')->where('cart_user_id',session('user')->id)->update(['cart_status'=>1]);
           if($res){
               $result = ['res'=>'error','msg'=>'购物车更新成功!'];
           }else{
               $result = ['res'=>'error','msg'=>'购物车更新失败!'];
           }
       }else{
           $result = ['res'=>'error','msg'=>'全选未记录!'];
       }

       echo json_encode($result);
   }

    /**
     * 购物车删除
     * @id 购物车id
     */
    public function destroy($id)
    {
        $res = $this->shopcart->destroy($id);
        if($res){
            $result = ['res'=>'ok','msg'=>'删除成功!'];
            return json_encode($result);
        }else{
            $result = ['res'=>'error','msg'=>'删除失败!'];
            return json_encode($result);
        }
    }

    /**购物车结算*/
    public function confirmorder()
    {
        $this->side = '购物车';
        $this->user =User::find(session('user')->id);
        $param['cart'] = CommonController::shopcart(session('user')->id);
        /**购物车数据显示*/
        $this->cart = Shopcart::where('cart_user_id',session('user')->id)->get();
        $param['cartlist'] = $this->cart;
        /**收货地址查看*/
        $param['addr'] = $this->addr->lists(session('user')->id);
        /**信用额度*/
        $param['user_quota']=DB::table('quota')->where('quota_user_id',session('user')->id)->first();
        /**有效优惠券*/
        $counpon_param['userid']=session('user')->id;
        $param['confirm_coupon'] = $this->coupon->coupon_useable($counpon_param);
        /**颜色*/
        $param['color'] = $this->color;
        
        return view('confirmorder',['user'=>$this->user,'nav'=>$this->nav,'param'=>$param]);
    }

}
